<?php
/**
 * @ClassName: Agentaward
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-07 17:00
 * @Version: V1.0.0
 */

namespace app\admin\controller\agent;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Agentaward extends Controller
{
    use HasBuilder;

    protected $dataModel;

    /**
     * 初始化
     **/
    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\AgentAward();
        $this->pageTitle = '推客奖励';
        $this->pagesize = 10;

        $this->selectTextField = '{name}';
        $this->selectFields = 'id,name';
        $this->selectSearch = 'id|name';
    }

    /**
     * 构建搜索
     *
     * @return void
     **/
    protected function buildSearch()
    {
        $search = $this->search;
        $search->select('type', '奖励类型')->options(model\AgentAward::$types);
        $search->text('name', '名称');
    }

    /**
     * 构建搜索条件
     *
     * @return $where
     **/
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['type'])) {
            $where[] = ['type', 'eq', $searchData['type']];
        }
        if (isset($searchData['name']) && $searchData['name'] != '') {
            $where[] = ['name', 'like', '%' . trim($searchData['name']) . '%'];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     **/
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('type_name', '奖励类型');
        $table->show('name', '名称');
        $table->show('level', '等级');
        $table->show('start', '最小数量');
        $table->show('end', '最大数量');
        $table->show('prices', '奖金');
        $table->switchBtn('enable', '开关')->autoPost('', true);
        $table->show('description', '描述');

        $table->fields('create_times', '申请/修改时间')->with(
            $table->show('create_time', '添加时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->select('type', '奖励类型')->options(model\AgentAward::$types);
        $form->text('name', '名称');
        $form->number('level', '等级');
        $form->number('start', '最小数量');
        $form->number('end', '最大数量');
        $form->number('prices', '奖金');
        $form->switchBtn('enable', '开关')->default(1);
        $form->textarea('description', '描述');
        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'type',
            'name',
            'level',
            'start',
            'end',
            'prices',
            'enable',
            'description',
        ], 'post');

        $result = $this->validate($data, [
            'type|类型' => 'require|number',
            'name|名称' => 'require',
            'level|等级' => 'require|number'
        ]);

        if (true !== $result) {
            $this->error($result);
        }

        if ($id) {
            $data['update_time'] = date_now();
        } else {
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

    /**
     * 招商奖分页
     * @return void
     */
    function selectAttractPage()
    {
        $selected = input("selected");
        $q = input("q");

        // a.组装条件
        $where = [];
        $where[] = ["type", "=", 1];
        if ($q) {
            $where[] = ['id|name|level', 'like', '%' . $q . '%'];
        }
        if ($selected) {
            $where[] = ['id', '=', $selected];
        }

        // b.页码
        $per_page = 50;
        $page = input('page') ? input('page') : 1;
        $from_page = $per_page * ($page - 1);

        // c.获取数据列表
        $list = model\AgentAward::where($where)->order('level')->limit($from_page, $per_page)->select();

        // d.是否还有下一页
        $has_more = 1;
        if (count($list) < $per_page) {
            $has_more = 0;
        }

        // 遍历列表，处理格式
        foreach ($list as $key => $item) {
            // 选中获取的值
            $item['__id__'] = $item['id']; // 设置选中值

            // 文本显示
            $item['__text__'] = $item['name'];

            $list[$key] = $item;
        }

        // 组装数据返回
        $data = [];
        $data['data'] = $list;
        $data['has_more'] = $has_more;
        echo json_encode($data);
    }

    /**
     * 运营奖分页
     * @return void
     */
    function selectOperationPage()
    {
        $selected = input("selected");
        $q = input("q");

        // a.组装条件
        $where = [];
        $where[] = ["type", "=", 3];
        if ($q) {
            $where[] = ['id|name|level', 'like', '%' . $q . '%'];
        }
        if ($selected) {
            $where[] = ['id', '=', $selected];
        }

        // b.页码
        $per_page = 50;
        $page = input('page') ? input('page') : 1;
        $from_page = $per_page * ($page - 1);

        // c.获取数据列表
        $list = model\AgentAward::where($where)->order('level')->limit($from_page, $per_page)->select();

        // d.是否还有下一页
        $has_more = 1;
        if (count($list) < $per_page) {
            $has_more = 0;
        }

        // 遍历列表，处理格式
        foreach ($list as $key => $item) {
            // 选中获取的值
            $item['__id__'] = $item['id']; // 设置选中值

            // 文本显示
            $item['__text__'] = $item['name'];

            $list[$key] = $item;
        }

        // 组装数据返回
        $data = [];
        $data['data'] = $list;
        $data['has_more'] = $has_more;
        echo json_encode($data);
    }
}